<template>
  <div>
    <video-player
      :url="url"
      :poster="poster"
      :can-play="canPlay"
      :mask-info="maskInfo"
    />
  </div>
</template>

<script>
import VideoPlayer from '@/components/video-player/VideoPlayer.vue'
import { apiGetPlayUrl } from '@/api/bangumi.js'
export default {
  name: 'BangumiPlayer',
  components: {
    VideoPlayer
  },
  props: {
    info: {
      type: Object
    }
  },
  data () {
    return {
      url: ''
    }
  },
  computed: {
    episodeId () {
      return this.hasInfo ? this.info.epInfo.id : null
    },
    hasInfo () {
      return this.info !== null
    },
    poster () {
      return this.hasInfo ? this.info.epInfo.cover : ''
    },
    limitCode () {
      let t = 0
      const info = this.info
      const epInfo = info.epInfo
      if (info.area === 1) {
        t = 1
      } else if (info.area === 3) {
        t = 4
      } else if (this.info.ssId === -1) {
        t = 3
      } else if (this.info.epList.length === 0 && this.info.pubInfo.is_started === 0) {
        t = 2
      } else if (epInfo.from !== 'bangumi' && epInfo.from !== 'vupload') {
        t = 5
      } else if ([6, 7, 8, 9, 12, 13].indexOf(epInfo.status) >= 0) {
        t = 6
      } else if (epInfo.archive_attr) {
        t = 7
      }
      return t
    },
    epStatus  () {
      return this.info.epInfo.status || this.info.meidaInfo.status
    },
    isFree  () {
      return [6, 7, 8, 9, 12, 13].indexOf(this.epStatus) < 0
    },
    canPlay () {
      if (!this.info.epInfo.id) {
        return !1
      }
      if (this.info.epType === 1) {
        if ([1, 2, 3, 5, 7].indexOf(this.limitCode) >= 0) {
          return !1
        }
      }
      return !(!this.isFree || this.info.epInfo.archive_attr) || (!this.isFree && !this.info.epInfo.archive_attr && !!this.info.rightsInfo.is_preview)
    },
    maskInfo () {
      let str = ''
      switch (this.limitCode) {
        case 1:
          str = '<div>非常抱歉，根据版权方要求</div><div>您所在的地区无法观看本片</div>'
          break
        case 2:
          if (this.info.epType !== 2) {
            str = this.info.pubInfo.unknow_pub_date === 1 ? '' : `${this.info.pubInfo.pub_time_show}即将开播`
          }
          break
        case 3:
          str = '地球上找不到该内容哦 (╥﹏╥)'
          break
        case 5:
          str = '该视频仅支持客户端观看'
          break
        case 7:
          str = '<div>本集为互动视频</div><div>请使用最新版APP或网页观看</div>'
          break
      }

      if (!this.isFree) {
        str = '<div>本片为付费内容</div><div>请前往App购买观看</div>'
      }

      return str
    }
  },
  watch: {
    episodeId (newVal) {
      if (newVal) {
        this.getVideoPlayUrl()
      }
    }
  },
  created () {
    this.getVideoPlayUrl()
  },
  methods: {
    getVideoPlayUrl () {
      if (this.episodeId && this.canPlay) {
        apiGetPlayUrl({ id: this.episodeId }).then(res => {
          const { code, result } = res
          if (code === 0 && result !== null) {
            const { durl } = result
            this.url = durl[0].url
            // this.videoDuration = timelength / 1000
          }
        }).catch(err => {
          console.log(err)
        })
      }
    }
  }
}
</script>

<style>

</style>
